package com.clayecloud.module_report_repair.viewModels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_common.apis.ReportRepairApi;
import com.clayecloud.lib_common.apis.entity.reportRepair.ClientReportRepair;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ReportRepairMainViewModel extends ViewModel implements ToastAction {

    private ReportRepairApi reportRepairApi;

    private Integer rrtype;

    private  MutableLiveData<Boolean> rrViewState;

    /*************************SETTER-GETTER********************/

    public Integer getRrtype() {
        return rrtype;
    }

    public void setRrtype(Integer rrtype) {
        this.rrtype = rrtype;
    }

    public MutableLiveData<Boolean> getRrViewState() {
        return rrViewState;
    }

    public void setRrViewState(MutableLiveData<Boolean> rrViewState) {
        this.rrViewState = rrViewState;
    }

    /*************************END*****************************/

    public void leadData(){
        //初始化网络接口
        reportRepairApi = HttpUtil.getInstance().create(ReportRepairApi.class);
        setRrtype(0);
        MutableLiveData<Boolean> rrViewState = new MutableLiveData<>();
        rrViewState.setValue(false);
        setRrViewState(rrViewState);
    }

    public void submit(String address, String username, String phone, String content){
        reportRepairApi.insert(new ClientReportRepair(this.rrtype, address, username, phone, content, (long) LoginUtil.getUserID()))
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            MutableLiveData<Boolean> rrViewState = getRrViewState();
                            rrViewState.setValue(true);
                            setRrViewState(rrViewState);
                        }else{
                            toast("添加失败，请从新提交");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络请求失败，请联系管理员");
                        Log.e("ReportRepairMainViewModel submit error",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }


}
